/**
 * 
 */
package string;

/**
 * Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.

For example,
"A man, a plan, a canal: Panama" is a palindrome.
"race a car" is not a palindrome.

Note:
Have you consider that the string might be empty? This is a good question to ask during an interview.

For the purpose of this problem, we define empty string as valid palindrome.
 * 
 * @author xyyi
 * @since   2013/01/20
 * @version 2013/01/20
 */
public class ValidPalindrome {

	/**
	 * 
	 */
	public ValidPalindrome() {
		// TODO Auto-generated constructor stub
	}

	public boolean isPalindrome(String s) {
		if (s == null || s.isEmpty()) {
			return true;
		}

		int left = 0;
		int right = s.length() - 1;
		while (left < right) {
			if (!Character.isLetterOrDigit(s.charAt(left))) {
				left++;
			} else if (!Character.isLetterOrDigit(s.charAt(right))) {
				right--;
			} else if (Character.toLowerCase(s.charAt(left)) != Character
					.toLowerCase(s.charAt(right))) {
				return false;
			} else {
				left++;
				right--;
			}
		}

		return true;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
